<?php echo $this->Session->flash(); ?>
<?php echo $this->Html->script('jquery.dataTables.js'); ?>
 

<script type="text/javascript">
    $(document).ready(function() {
        $('#browserList').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "<?php echo $this->Html->Url('/Sales/salesdata'); ?>"
        });
    });
</script>

<div id="TB_load"><?php echo $this->Html->image("loading_animation.gif")?></div>
<div class="sale_item">

		<div id=breadcrumbAdmin>
	    	<?php $this->Html->addCrumb('Sales', '/Sales/index', array('style'=> ('text-decoration:underline; color:#EB1E70;'))); ?>
	        <?php echo $this->Html->getCrumbs(' > ', array( 'text' => 'Transactions', 'url' => array('controller' => 'Sales', 'action' => 'index', 'plugin' => null), 'escape' => false)); ?>
	    </div>

	    <h2 style="padding:0.5%;"><?php echo __('Sales'); ?></h2>
	   <table id="browserList" style="width:120%;" cellpadding="0" cellspacing="0" class="items">
    <thead>
        <tr>
				<th>Sale ID</th>
				<th>Date</th>
				<th>Customer</th>
				<th>NO. of item</th>
				<th>Original Price</th>
				<th>Sale Price</th>
				<th>Sales Receipt</th>
				<th>Refund</th>
				<th>More information</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="4" class="dataTables_empty">Loading data from server...</td>
        </tr>
    </tbody>
</table>

<!--	<table class="items">
		<thead>
			<tr>
				<th>Sale ID</th>
				<th>Date</th>
				<th>Customer</th>
				<th>NO. of item</th>
				<th>Original Price</th>
				<th>Sale Price</th>
				<th>Sales Receipt</th>
				<th>Refund</th>
				<th>More information</th>
			</tr> 
		</thead>
		<tbody>
		<?php //if(!empty($sales)){?>
		<?php //foreach($sales as $sale){?>
			<tr id="sale_id_<?php //echo $sale["Sale"]["id"]?>">
				<td><?php //echo $sale["Sale"]["id"]?></td>
				<td><?php //echo $this->Time->format("Y-m-d",$sale["Sale"]["created"])?></td>
				<td><?php //echo $sale["Customer"]["first_name"]." ".$sale["Customer"]["last_name"]?></td>
				<td><?php //echo count($sale["SaleItem"])?></td>
				<td><?php //echo $this->Number->currency($sale["Sale"]["sale_original_price"],"USD")?></td>
				<td><?php //echo $this->Number->currency($sale["Sale"]["sale_price"],"USD",array("negative"=>""))?></td>
				<td>
					<a class="btn sale_receipt" href="javascript:;" data-id="<?php //echo $sale["Sale"]["id"]?>">Receipt</a>
				</td>
				<td>
				    <a class="btn sale_refund" href="javascript:;" data-id="<?php //echo $sale["Sale"]["id"]?>">Refund</a>
				</td>
				<td>
                    <a class="btn sale_view" href="javascript:;" data-id="<?php //echo $sale["Sale"]["id"]?>">View more</a>
                </td>
			</tr>
			<?php //}}?>
		</tbody>
	</table>

	<div class="paging">-->
        <?php
      /*  echo $this->Paginator->counter(array(
        'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
        ));*/
        ?>
	<?php
		//echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
	//	echo $this->Paginator->numbers(array('separator' => ''));
	//	echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
	?>
	<!--</div>-->
</div>
<div id="item_refund_dialog" title="Refund">
		<div id="item_refund_li"></div>
</div>
<div id="item_viewmore" title="View more">
		<div id="item_viewmore_li"></div>
</div>
<?php echo $this->Html->css('/css/ui-lightness/jquery'); ?>
<?php echo $this->Html->script('jquery-ui'); ?>
<?php echo $this->Html->script('jquery.form.min'); ?>
<script type="text/javascript">
	function to(url) {
		window.location.href = url;
	}
	$(document).ready(function(){
		$(".sale_view").click(function(){
			var sale_id=$(this).data("id");
			var sale_add=$(this).data("add");
			$(".layby_list_view").hide();
			if(!sale_add){
				$.get("<?php echo $webroot?>Sales/viewsale/"+sale_id,function(data){
					$("#sale_id_"+sale_id).after(data).next().show(500);
				});
				$(this).data("add",true);
			}else{
				$("#sale_view_"+sale_id).show(500);
			}
		});
		//check sale and continue payment,, not needed now
		$(".sale_unsuspend").click(function(){
			to("<?php echo $webroot?>Sales/unsuspend/"+$(this).data("id"));
		});
		//when clicking for the receipt button
		$(".sale_receipt").click(function(){
			to("<?php echo $webroot?>Sales/receipt/"+$(this).data("id"));
		});
		//refund
		$(".sale_refund").click(function(){
			$( "#item_refund_dialog" ).dialog( "open" );
			$("#item_refund_li").empty();
			//add item
			$.get("<?php echo $webroot?>Sales/sale_items/"+$(this).data("id"),function(data){
				$("#item_refund_li").append(data);
			});
			return false;
		});
		$("#item_refund_dialog").dialog({
			autoOpen: false,
			resizable:false,
			dialogClass:"item-refund-dialog",
			width:650,
			modal:true,
			buttons:{
				"Finish":function(){
					//finished refunding, submit refund detail
					if($("#item_refund_dialog .refunded").length>0){
						var item_ids ="";
						$("#item_refund_dialog .refunded").each(function(){
							item_ids+=$(this).data("id")+";";
						});
						if(item_ids){
							item_ids=item_ids.substring(0,item_ids.length-1);
						}
						$("#item_refund_dialog input[name='item_id']").val(item_ids);
						$('#refund_form').ajaxSubmit({target: "#main", beforeSubmit: salesBeforeSubmit, success: salesSuccess});
					}
				},"Cancel":function(){
					$(this).dialog("close");
				}}
		});
		
		$("#item_viewmore").dialog({
			autoOpen: false,
			resizable:false,
			dialogClass:"item-viewmore-dialog",
			width:650,
			modal:true,
			buttons:{
				"Cancel":function(){
					$(this).dialog("close");
				}}
		});
		//close dialogue box automatically
		setTimeout(function(){$("#flashMessage").hide();},5000);
	});
function salesBeforeSubmit(formData, jqForm, options)
{
	$("#add_payment_button").hide();
	$("#item_refund_dialog").dialog("close");
	$("#TB_load").show();
}
function refundpopup(id)
{
	$( "#item_refund_dialog" ).dialog( "open" );
	$("#item_refund_li").empty();
	//add item
	$.get("<?php echo $webroot?>Sales/sale_items/"+id,function(data){
		$("#item_refund_li").append(data);
	});
	return false;
}
function viewmoretable(sale_id)
{
	$( "#item_viewmore" ).dialog( "open" );
	$("#item_viewmore_li").empty();
	//add item
	$.get("<?php echo $webroot?>Sales/viewsale/"+sale_id,function(data){
			$("#item_viewmore_li").append(data);
    	    $(".layby_list_view").show();
		});
	return false;
}
function salesSuccess(responseText, statusText, xhr, $form)
{
}
</script>
